/**
 * 适配器，适配一些不兼容的类或函数
 * 
 */
interface Result {
  success: boolean
  data?: Array<any>
  message?: string
}

interface Api {
  sdk: any
  describeInstances(): Result
}

// 待适配的sdk
class AliyunSdk {
  describeInstances (): any {
    return {success: 'ok', vms: ['ali']}
  }
}

class HuaweiSdk {
  describeVms (): any {
    return {code: 200, instances: ['huawei']}
  }
}

// 适配器
class HuaweiClient implements Api {

  constructor (public sdk: HuaweiSdk) {}

  describeInstances (): Result {
    let result = this.sdk.describeVms()
    let outputReuslt: Result = {
      success: result.code == 200 ? true : false
    }
    if (outputReuslt.success) {
      outputReuslt['data'] = result["instances"]
    } else {
      outputReuslt['message'] = result["message"]
    }
    return outputReuslt
  }
}


class AliyunClient implements Api {

  constructor (public sdk: AliyunSdk) {}

  describeInstances (): Result {
    let result = this.sdk.describeInstances()
    let outputReuslt: Result = {
      success: result.success == "ok" ? true : false
    }
    if (outputReuslt.success) {
      outputReuslt['data'] = result["vms"]
    } else {
      outputReuslt['message'] = result["message"]
    }
    return outputReuslt
  }
}

function main__ () {
  let apiClient: Api = new AliyunClient(new AliyunSdk())
  let result: Result = apiClient.describeInstances()
  console.log(result)

  apiClient = new HuaweiClient(new HuaweiSdk())
  result = apiClient.describeInstances()
  console.log(result)
}


main__()